<?php
namespace app\beautiful\controller\admin\setting;
use app\beautiful\model\setting\SettingBuyModel;
use app\BaseController;

class Buy extends BaseController{
    public function show()
    {
        if(null === $buy_setting=SettingBuyModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $buy_setting = new SettingBuyModel();
            $buy_setting->shop_id=$this->request->shopId;
            $buy_setting->save();
        }
        $detail=[
            'is_open'=>$buy_setting->is_open,
            'is_use_coupon'=>$buy_setting->is_use_coupon,
            'is_integral_deduction_balance'=>$buy_setting->is_integral_deduction_balance,
            'is_vip_discount'=>$buy_setting->is_vip_discount,
        ];
        return $this->success(['detail'=>$detail,'miniapp_status' => $this->request->shop->status]);
    }

    public function save()
    {
        $is_open=$this->request->param('is_open/d',0);
        $is_use_coupon=$this->request->param('is_use_coupon/d',0);
        $is_integral_deduction_balance=$this->request->param('is_integral_deduction_balance/d',0);
        $is_vip_discount=$this->request->param('is_vip_discount/d',0);

        if(!in_array($is_open,[0,1])) return $this->error('参数错误');
        if(!in_array($is_use_coupon,[0,1])) return $this->error('参数错误');
        if(!in_array($is_integral_deduction_balance,[0,1])) return $this->error('参数错误');
        if(!in_array($is_vip_discount,[0,1])) return $this->error('参数错误');

        if(null === $buy_setting=SettingBuyModel::where([['shop_id','=',$this->request->shopId]])->find()){
            $buy_setting = new SettingBuyModel();
            $buy_setting->shop_id=$this->request->shopId;
        }

        $buy_setting->is_open=$is_open;
        $buy_setting->is_use_coupon=$is_use_coupon;
        $buy_setting->is_integral_deduction_balance=$is_integral_deduction_balance;
        $buy_setting->is_vip_discount=$is_vip_discount;
        $buy_setting->save();

        return $this->success('保存成功');
    }
}